const app = getApp();
const util = require("../../../utils/util.js");
const rq = require("../../../utils/https.js");
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    //导航栏
    TabCur: 0,
    scrollLeft: 0,
    tab: ['单次预约', '多次预约'],
    //表格参数
    loading: false,
    users: [],
    rooms: [], //会议室
    roomIndex: null,
    important: false, //是否重要
    date: '', //日期
    user_id: null,
    user_name: null,
    user_ids: [],
    user_names: null,
    start_time: '09:00',
    end_time: '09:00',
    modalName:null,

  },
  onLoad() {
    //设置今天的日期
    this.setData({
      today: util.formatTime(new Date()).substring(0, 10)
    })
    this.setData({
      users: wx.getStorageSync('users'),
      rooms: wx.getStorageSync('rooms'),
      user_id: wx.getStorageSync('user_id'),
      user_name: wx.getStorageSync('user_name'),
      user_ids: [wx.getStorageSync('user_id')],
      user_names: wx.getStorageSync('user_name'),
    })
  },
  onShow() {
    //单个
    let tmp_user = wx.getStorageSync('tmp_user');
    wx.removeStorage({
      key: 'tmp_user'
    })
    if (tmp_user) {
      let user_name = '';
      let users = this.data.users;
      for (let i = 0; i < users.length; i++) {
        if (users[i].user_id == tmp_user) {
          user_name = users[i].name;
          break;
        }
      }
      this.setData({
        user_id: tmp_user,
        user_name: user_name,
      })
      return;
    }
    let tmp_users = wx.getStorageSync('tmp_users');
    wx.removeStorage({
      key: 'tmp_users'
    })
    if (tmp_users) {
      let user_names = [];
      let users = this.data.users;
      for (let i = 0; i < users.length; i++) {
        if (tmp_users.indexOf(users[i].user_id) != -1) {
          user_names.push(users[i].name);
        }
      }


      this.setData({
        user_ids: tmp_users,
        user_names: user_names.join(','),
      })
    }
  },
  formSubmit: function(e) {
    console.log(e.detail.value)
    let params = e.detail.value;
    params.user_ids = params.user_ids.split(',')
    rq.request('meeting/apply', params, function(result) {
      if (result.code == 0) {
        wx.showToast({
          title: result.msg,
        })
        // setTimeout(function() {
        //   wx.navigateBack({
        //     delta: 1
        //   })
        // }, 2000)
      } else {
        wx.showToast({
          title: result.msg,
          icon: 'none'
        })
      }
    })
  },
  //tab方法
  TabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      scrollLeft: (e.currentTarget.dataset.id - 1) * 60
    })
  },
  //是否重要修改
  ImportantChange(e) {
    this.setData({
      important: e.detail.value
    })
  },
  //日期修改
  //todo 选择时间
  DateChange(e) {
    this.setData({
      date: e.detail.value
    })
  },
  //选择主持人
  ChoosePeople() {
    wx.navigateTo({
      url: '../../public/selectUser/single/single',
    })
  },
  ChoosePeoples() {
    wx.navigateTo({
      url: '../../public/selectUser/multiple/multiple?tmp_users=' + this.data.user_ids,
    })
  },
  //选择完成后提示当天已有的安排
  PickerChange(e) {
    let that = this;
    let roomIndex = e.detail.value
    let room_id = this.data.rooms[roomIndex].room_id;
    let date = this.data.date
    if(date){
      rq.request('meeting/date',{room_id:room_id,date:date},function(result){
        if(result.code==0){
          console.log(result.data);
          that.showModal(result.data);
        }else{
          wx.showToast({
            title: result.msg,
            icon:'none'
          })
        }
      })
    }
    this.setData({
      roomIndex: roomIndex
    })
  },
  DateChange(e) {
    let that = this;
    let date = e.detail.value
    console.log(this.data.roomIndex)
    if (this.data.roomIndex){
      let room_id = this.data.rooms[this.data.roomIndex].room_id
      rq.request('meeting/date',{room_id:room_id,date:date},function(result){
        if (result.code == 0) {
          console.log(result.data);
          that.showModal(result.data);
        }else{
          wx.showToast({
            title: result.msg,
            icon: 'none'
          })
        }
      })
    }
    this.setData({
      date: date
    })
  },
  showModal(data){
    console.log(data);
    if (data.length){
      this.setData({
        date_arrange:data,
        modalName: 'Modal'
      })
    }else{
      wx.showToast({
        title: '可预约',
      })
    }
  },
  hideModal(e){
    this.setData({
      modalName: null
    })
  },


  StartTimeChange(e) {
    let start_time = e.detail.value;
    if(this.data.end_time<start_time){
      this.setData({
        end_time:start_time
      })
    }
    this.setData({
      start_time: start_time
    })
  },
  EndTimeChange(e) {
    let end_time = e.detail.value;
    this.setData({
      end_time: end_time
    })
  },
})